﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acme.Sakila.TestSite.Objects;
using Acme.Sakila.EFDAL;
using Acme.Sakila.EFDAL.Entity;

namespace Acme.Sakila.TestSite
{
	public partial class PaymentItem : BasePersistablePage
	{
		#region Page Events

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			cmdSave.Click += new EventHandler(SaveData);
			cmdCancel.Click += new EventHandler(cmdCancel_Click);
		}

		#endregion

		#region Methods

		protected override nHydrate.EFCore.DataAccess.NHEntityObject CreateObject(SakilaEntities context)
		{
			var newItem = new Acme.Sakila.EFDAL.Entity.payment();
			context.AddItem(newItem);
			return newItem;
		}

		protected override void SetupBindings(SakilaEntities context)
		{
			var paymentId = this.Request["id"].ToInt32();
			var thePayment = context.payment.FirstOrDefault(x => x.payment_id == paymentId);
			if (thePayment == null)
				thePayment = CreateObject(context) as Acme.Sakila.EFDAL.Entity.payment;

			Mapper.Map(lblOrderId, thePayment, Acme.Sakila.EFDAL.Entity.payment.FieldNameConstants.payment_id);
			Mapper.Map(txtAmount, thePayment, Acme.Sakila.EFDAL.Entity.payment.FieldNameConstants.amount, lblAmount);
			Mapper.Map(txtPaymentDate, thePayment, Acme.Sakila.EFDAL.Entity.payment.FieldNameConstants.payment_date, lblPaymentDate);

			#region Customer
			linkCustomer.Text = "(NOT SET)";
			linkCustomer.NavigateUrl = "#";
			if (thePayment.customer != null)
			{
				linkCustomer.Text = thePayment.customer.ToString();
				linkCustomer.NavigateUrl = "/CustomerItem.aspx?id=" + thePayment.customer_id;
			}
			#endregion

			#region Rental
			linkRental.Text = "(NOT SET)";
			linkRental.NavigateUrl = "#";
			if (thePayment.rental != null)
			{
				linkRental.Text = "[" + thePayment.rental.rental_id.ToString() + "]";
				linkRental.NavigateUrl = "/RentalItem.aspx?id=" + thePayment.rental_id;
			}
			#endregion

		}

		#endregion

		#region Event Handlers

		private void cmdCancel_Click(object sender, EventArgs e)
		{
			this.Response.Redirect(SessionHelper.LastPaymentListSearch);
		}

		protected override void OnDataSavedSucess(EventArgs e)
		{
			base.OnDataSavedSucess(e);
			this.Response.Redirect(SessionHelper.LastPaymentListSearch);
		}

		#endregion

	}
}